package com.daykw.xiaohongshu.task.thread;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daykw.xiaohongshu.http.HttpBase;
import com.daykw.xiaohongshu.model.TArticleTask;
import com.daykw.xiaohongshu.model.TCommentInfo;
import com.daykw.xiaohongshu.model.UpdateAid;
import com.daykw.xiaohongshu.mq.TQueue;
import com.daykw.xiaohongshu.util.Const;
import com.daykw.xiaohongshu.util.IPPoolMap;
import com.daykw.xiaohongshu.util.MongoDBUtil;
import com.daykw.xiaohongshu.util.SpringContextHolder;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.InsertManyOptions;

public class RestCommentThread extends Thread {
	private static final Logger logger = Logger.getLogger(RestCommentThread.class);
	private UpdateAid info;

	public RestCommentThread(UpdateAid info) {
		this.info = info;
	}

	@Override
	public void run() {
		consume();

	}

	public synchronized void consume() {
		NutDao xhsDao = SpringContextHolder.getBean("dao");
		SimpleDateFormat fdate = new SimpleDateFormat("yyyyMMddHH");
		SimpleDateFormat fpdate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd");
		logger.info("****重置评论线程启动消息*****");

		try {

			xhsDao.update(UpdateAid.class, Chain.make("comment_status", 1), Cnd.where("aid", "=", info.getAid()));

			int nowdate = Integer.parseInt(fdate.format(new Date()));

			// String url = "https://www.xiaohongshu.com/api/sns/v5/note/" +
			// info.getAid()
			// +
			// "/comment/list?start=5a5e14c5b42c8a64b10c34c1&num=20&show_priority_sub_comments=0&platform=Android&deviceId=2ba5c049-33de-3111-bfc3-2dd4b662d396&versionName=5.9.0&channel=Xiaomi&sid=session.1201873076495524263&lang=zh-CN&t=1517377263&sign=65d667de3065bbe68503791462f66e98";//
			// info.getUrl();
			java.util.Random r = new java.util.Random();
			String article_id = info.getAid();
			try {
				Map<String, String> headers = new HashMap<String, String>();
				headers.put("Accept", "*/*");
				headers.put("Host", "www.xiaohongshu.com");
				headers.put("Content-Type", "application/json");
				headers.put("Accept", "*/*");
				headers.put("User-Agent",
						"Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A5339e MicroMessenger/6.7.1 NetType/WIFI Language/zh_CN");
				headers.put("Referer", "https://servicewechat.com/wxffc08ac7df482a27/"+r.nextInt(100)+"/page-frame.html");
				headers.put("Accept-Language", "zh-cn");
				headers.put("Accept-Encoding", "br, gzip, deflate");


				
				String url = "http://www.xiaohongshu.com/web_api/sns/v1/note/" + article_id
						+ "/comment?page_size=1000&sid=session.1" + r.nextInt(224697449) + ""
						+ r.nextInt(992265726);

				String comment_html = new HttpBase().get(url, "utf-8", headers, true);
				while (StringUtils.isNotEmpty(comment_html) && comment_html.equals("fail")) {

					comment_html = new HttpBase().get(url, "utf-8", headers, true);

					if (comment_html.equals("404")) {
						break;
					}

					// TimeUnit.SECONDS.sleep(3);
					// logger.info("##########请求返回Fail##########");
				}

				if (StringUtils.isNotEmpty(comment_html) && comment_html.equals("404")) {
					xhsDao.update(UpdateAid.class, Chain.make("comment_status", 4), Cnd.where("id", "=", info.getId()));

					return;
				}
				// logger.info("##########请求评论返回成功##########");
				JSONObject commentjson = null;
				
				try {
					commentjson = JSONObject.parseObject(comment_html);
				} catch (Exception e) {
					xhsDao.update(UpdateAid.class, Chain.make("comment_status", 3), Cnd.where("id", "=", info.getId()));

					return;
				}

				if (commentjson == null || commentjson.isEmpty()) {
					xhsDao.update(UpdateAid.class, Chain.make("comment_status", 3), Cnd.where("id", "=", info.getId()));

					return;
				} else {
					if (!commentjson.getBoolean("success")) {
						xhsDao.update(UpdateAid.class, Chain.make("comment_status", 3),
								Cnd.where("id", "=", info.getId()));

						return;
					}
				}

				MongoCollection<org.bson.Document> coll = MongoDBUtil.instance.getCollection(Const.MDB_DB, "comment");

				JSONObject cmj = commentjson.getJSONObject("data");
				if (cmj.getInteger("total") == null || cmj.getInteger("total") == 0) {
					xhsDao.update(UpdateAid.class, Chain.make("comment_status", 2),
							Cnd.where("id", "=", info.getId()));
					return;
				}

				JSONArray array = cmj.getJSONArray("comments");
				if (array == null || array.size() < 1) {
					xhsDao.update(UpdateAid.class, Chain.make("comment_status", 4), Cnd.where("id", "=", info.getId()));

					return;
				}
			
				List<org.bson.Document> docs = new ArrayList<org.bson.Document>();
				for (int i = 0; i < array.size(); i++) {
					JSONObject c = array.getJSONObject(i);
					String comment_id = c.getString("id");
					String comment_time = c.getString("time");
					TCommentInfo cinfo = new TCommentInfo();
					try {

						cinfo.setArticle_id(article_id);

						try {
							Date pdate = fpdate.parse(comment_time);
							// pdate.setTime(Long.parseLong(comment_time) * 1000);
							cinfo.setPub_date(pdate);
							cinfo.setPdate(Integer.parseInt(date.format(pdate)));
						} catch (Exception de) {
							xhsDao.update(UpdateAid.class, Chain.make("comment_status", 3),
									Cnd.where("id", "=", info.getId()));
							return;

						}
						cinfo.setCreate_date(nowdate);
						cinfo.setComment_id(comment_id);
						cinfo.setUrl(url);
						// cinfo.setJson(c.toJSONString());
						xhsDao.fastInsert(cinfo);
						logger.info("######消费完一条评论" + info.getAid() + "评论队列剩余:" + TQueue.getCQueue().size() + ",IP:"
								+ IPPoolMap.getQueue().size());
					} catch (Exception e1) {
						// logger.error("", e1);
						// xhsDao.update(TArticleTask.class,
						// Chain.make("status", 3),
						// Cnd.where("id", "=", info.getId()));
					}

					org.bson.Document doc = org.bson.Document.parse(c.toJSONString());
					doc.put("aid", article_id);
					doc.put("cdate", nowdate);
					doc.put("pdate", cinfo.getPdate());
					docs.add(doc);

				}

				try {
					if (docs != null && !docs.isEmpty()) {
						coll.insertMany(docs, new InsertManyOptions().ordered(false));
					}

					// xhsDao.update(TArticleTask.class,
					// Chain.make("comment_status", 2),
					// Cnd.where("id", "=", info.getId()));

				} catch (Exception e) {

					// xhsDao.update(TArticleTask.class,
					// Chain.make("comment_status", 2),
					// Cnd.where("id", "=", info.getId()));

				}

			
					xhsDao.update(UpdateAid.class, Chain.make("comment_status", 2), Cnd.where("id", "=", info.getId()));
				

			} catch (Exception e) {

				xhsDao.update(UpdateAid.class, Chain.make("comment_status", 3), Cnd.where("id", "=", info.getId()));

				logger.error("解析html异常:aid:" + info.getAid(), e);
			}

		} catch (Exception e) {
			logger.error("", e);

		}

	}

	public static void main(String[] args) throws Exception {
		String html = new HttpBase().get("http://www.xiaohongshu.com/discovery/item/59872886d1d3b919909ace01", "utf-8",
				null, true);

		Document doc = Jsoup.parse(html);
		Elements es = doc.select("script");
		for (Element e : es) {
			String script = e.html();
			if (script.indexOf("__INITIAL_SSR_STATE__") >= 0) {
				String json = script.substring(script.indexOf("{"), script.lastIndexOf("}") + 1);
				System.out.println(json);

				JSONObject obj = JSONObject.parseObject(json);

			}
		}
		// System.out.println(es);
		// JSONArray array = parseService.parseVal(doc, requestId, url);
	}
}
